package com.xxy.test;


import java.util.Scanner;
/**
 * @author xxy
 * @create 2021 3 16 22:07
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
       int r=scanner.nextInt();
       int y=scanner.nextInt();
       int g=scanner.nextInt();
       int all=y+r+g;
       int sum=0;
       int n=scanner.nextInt();
       while (n>0){
           int k=scanner.nextInt();
           int t=scanner.nextInt();
           if(k==0){
               sum+=t;
           }else if(k==1){
               int temp=sum%all;
               if(temp<=t){
                   sum+=(t-temp);
               }else {
                   if(temp-t>g){
                       sum+=((y-(temp-t-g))+r);
                   }
               }
           } else if(k==2){
               int temp=sum%all;
               if(temp<=t){
                   sum+=(t-temp+r);
               }else {
                   if(temp-t<r) {
                      sum+=(r-temp+t);
                   }
               }
       }else {
               int temp=sum%all;
               if(temp>t){
                   if(temp-t>y){
                       sum+=(r-(temp-t-y));
                   }else {
                       sum+=(r+y-(temp-t));
                   }
               }

           }
           n--;
       }
        System.out.println(sum);
    }
}